package com.service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.model.ProxyIp;
import com.util.HTTPUtils;

public class ProxyIpService {
	
	
	public static List<ProxyIp> getData (String html) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//非线程安全
		List<ProxyIp> data = new ArrayList<ProxyIp>();
		
		Document doc = Jsoup.parse(html);
		Elements trdatas=doc.select("div[id=list]").select("tbody").select("tr");
		for (Element tr:trdatas) {
			ProxyIp proxyIp=new ProxyIp();
			String ip = tr.select("td[data-title=IP]").text();
			String port = tr.select("td[data-title=PORT]").text();
			String showType = tr.select("td[data-title=匿名度]").text();
			String requestType = tr.select("td[data-title=类型]").text();
			String location = tr.select("td[data-title=位置]").text();
			String responseTime = tr.select("td[data-title=响应速度]").text();
			String lastCheckStr = tr.select("td[data-title=最后验证时间]").text();
			Date lastCheck = sdf.parse(lastCheckStr);
			proxyIp.setIp(ip);
			proxyIp.setPort(port);
			proxyIp.setShowType(showType);
			proxyIp.setRequestType(requestType);
			proxyIp.setLocation(location);
			proxyIp.setResponseTime(responseTime);
			proxyIp.setLastCheck(lastCheck);
			data.add(proxyIp);
		}
		return data;
	}
	public static List<ProxyIp> parseProxyIp(String url){
		List<ProxyIp> iplist = new ArrayList<ProxyIp>();
		try {
			String response = HTTPUtils.getRawHtml(url); 
			if(response != null && response.length() > 0){
				iplist = getData(response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return iplist;
	}
	
	public static boolean ipAvilable(String ip,int port){
		boolean result = false;
		try {            
			URL url = new URL("http://www.baidu.com");            
			// 创建代理服务器            
			InetSocketAddress addr = new InetSocketAddress(ip,port);            
			Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理            
			// 如果我们知道代理server的名字, 可以直接使用          
			URLConnection conn = url.openConnection(proxy);  
			conn.setConnectTimeout(5000);
			conn.setReadTimeout(5000);
			
			InputStream in = conn.getInputStream();  
			InputStreamReader isr = new InputStreamReader(in,"UTF-8");
			BufferedReader reader = new BufferedReader(isr);
			String tempStr;
			StringBuilder stringBuilder = new StringBuilder();
			while ((tempStr = reader.readLine()) != null) {
				stringBuilder.append(tempStr);
			}
			String str = stringBuilder.toString();
			if(str != null && str != ""){
				Pattern p = Pattern.compile(".*030173.*");
				Matcher m = p.matcher(str);
				if (m.find()) {
					result = true;
				}
			}
		} catch (Exception e) {    
//				System.out.println("exception when connecting,ip:"+ip);  //会有很多的错误     
		}    
		
		return result ;
	}
}
